%% Calculates group averages for all subjects

clear all; close all;

savefolder = 'D:\Data\All subjects\';

%% VARIABLES

fast_subjs    = 8;      % total number of subjects tracking fast stimuli in session 1
slow_subjs    = 5;      % total number of subjects tracking slow stimuli in session 1
s2_fastsubjs  = 4;      % number of subjects that have undertaken a second experimental session with fast stimuli
s2_slowsubjs  = 2;      % number of subjects that have undertaken a second experimental session with slow stimuli

scnw          = 1600;   % screen width in pixels
scnwcm        = 41.2;   % screen width in cm
view_dist     = 60;     % subject distance from screen in cm

outlier       = 250;    % max RMSE over which trials are excluded from averages

delay         = 200;    % To calculate Residual predictive gain: amount of time (ms)
                        % discounted from initial part of target path in occluder as
                        % eye velocity is maintained for this period

sacvel        =  35;    % Cut-off for saccade velocity (deg/s) - all velocities 
                        % above this are excluded from the averages
               
smvp          =   1;    % Degree of smoothing for velocity (EXCL sac) pre-averaging
smve          = 100;    % Degree of smoothing for velocity (EXCL sac) post-averaging
smvi          = 100;    % Degree of smoothing for velocity INCL saccades
                        % (derived from average displacement)

%% LOAD DATA AND PUT FN/FS/SN/SS STRUCTURES WITH SUBJECT NUMBERS

loadallsubjdata

% allsubj = struct('FN',{},'FS',{},'SN',{},'SS',{});

allsubj.FN    = struct('one',{{}},'onevel',{{}},'onevelnosac',{{}});
allsubj.FS    = struct('one',{{}},'onevel',{{}},'onevelnosac',{{}});
allsubj.SN    = struct('one',{{}},'onevel',{{}},'onevelnosac',{{}});
allsubj.SS    = struct('one',{{}},'onevel',{{}},'onevelnosac',{{}});
allsubj.Stats = struct('RMSE',{{}},'gain',{{}},'velerr',{{}});

%  AVERAGE TRACES OVER ALL SUBJECTS: Displacement

allsubj.FN.one = ...
    (S2.FN.one + ...
    S3.FN.one + ...
    S4.FN.one + ...
    S6.FN.one + ...
    S7.FN.one + ...
    S9.FN.one + ...
    S10.FN.one + ...
    S11.FN.one)./fast_subjs;

session2.FN.one = ...
    (Session2.S2.FN.one + ...
    Session2.S3.FN.one + ...
    Session2.S4.FN.one + ...
    Session2.S9.FN.one)./s2_fastsubjs;

allsubj.FS.one = ...
    (S2.FS.one + ...
    S3.FS.one + ...
    S4.FS.one + ...
    S6.FS.one + ...
    S7.FS.one + ...
    S9.FS.one + ...
    S10.FS.one + ...
    S11.FS.one)./fast_subjs;

session2.FS.one = ...
    (Session2.S2.FS.one + ...
    Session2.S3.FS.one + ...
    Session2.S4.FS.one + ...
    Session2.S9.FS.one)./s2_fastsubjs;

allsubj.SN.one = ...
    (S2.SN.one + ...
    S3.SN.one + ...
    S4.SN.one + ...
    Session2.S6.SN.one + ...
    S9.SN.one)./slow_subjs;

session2.SN.one = ...
    (Session2.S2.SN.one + ...
    Session2.S6.SN.one)./s2_slowsubjs;

allsubj.SS.one = ...
    (S2.SS.one + ...
    S3.SS.one + ...
    S4.SS.one + ...
    Session2.S6.SS.one + ...
    S9.SS.one)./slow_subjs;

session2.SS.one = ...
    (Session2.S2.SS.one + ...
    Session2.S6.SN.one)./s2_slowsubjs;

%  AVERAGE TRACES OVER ALL SUBJECTS: Vel incl saccades

allsubj.FN.onevel = ...
    (S2.FN.onevel + ...
    S3.FN.onevel + ...
    S4.FN.onevel + ...
    S6.FN.onevel + ...
    S7.FN.onevel + ...
    S9.FN.onevel + ...
    S10.FN.onevel + ...
    S11.FN.onevel)./fast_subjs;

session2.FN.onevel = ...
    (Session2.S2.FN.onevel + ...
    Session2.S3.FN.onevel + ...
    Session2.S4.FN.onevel + ...
    Session2.S9.FN.onevel)./s2_fastsubjs;

allsubj.FS.onevel = ...
    (S2.FS.onevel + ...
    S3.FS.onevel + ...
    S4.FS.onevel + ...
    S6.FS.onevel + ...
    S7.FS.onevel + ...
    S9.FS.onevel + ...
    S10.FS.onevel + ...
    S11.FS.onevel)./fast_subjs;

session2.FS.onevel = ...
    (Session2.S2.FS.onevel + ...
    Session2.S3.FS.onevel + ...
    Session2.S4.FS.onevel + ...
    Session2.S9.FS.onevel)./s2_fastsubjs;

allsubj.SN.onevel = ...
    (S2.SN.onevel + ...
    S3.SN.onevel + ...
    S4.SN.onevel + ...
    S9.SN.onevel)./slow_subjs;

session2.SN.onevel = ...
    (Session2.S2.SN.onevel + ...
    Session2.S6.SN.onevel)./s2_slowsubjs;

allsubj.SS.onevel = ...
    (S2.SS.onevel + ...
    S3.SS.onevel + ...
    S4.SS.onevel + ...
    S9.SS.onevel)./slow_subjs;

session2.SS.onevel = ...
    (Session2.S2.SS.onevel + ...
    Session2.S6.SS.onevel)./s2_slowsubjs;

%  AVERAGE TRACES OVER ALL SUBJECTS: Vel excl saccades

allsubj.FN.onevelnosac = ...
    (S2.FN.onevelnosac + ...
    S3.FN.onevelnosac + ...
    S4.FN.onevelnosac + ...
    S6.FN.onevelnosac + ...
    S7.FN.onevelnosac + ...
    S9.FN.onevelnosac + ...
    S10.FN.onevelnosac + ...
    S11.FN.onevelnosac)./fast_subjs;

session2.FN.onevelnosac = ...
    (Session2.S2.FN.onevelnosac + ...
    Session2.S3.FN.onevelnosac + ...
    Session2.S4.FN.onevelnosac + ...
    Session2.S9.FN.onevelnosac)./s2_fastsubjs;

allsubj.FS.onevelnosac = ...
    (S2.FS.onevelnosac + ...
    S3.FS.onevelnosac + ...
    S4.FS.onevelnosac + ...
    S6.FS.onevelnosac + ...
    S7.FS.onevelnosac + ...
    S9.FS.onevelnosac + ...
    S10.FS.onevelnosac + ...
    S11.FS.onevelnosac)./fast_subjs;

session2.FS.onevelnosac = ...
    (Session2.S2.FS.onevelnosac + ...
    Session2.S3.FS.onevelnosac + ...
    Session2.S4.FS.onevelnosac + ...
    Session2.S9.FS.onevelnosac)./s2_fastsubjs;

allsubj.SN.onevelnosac = ...
    (S2.SN.onevelnosac + ...
    S3.SN.onevelnosac + ...
    S4.SN.onevelnosac + ...
    S9.SN.onevelnosac)./slow_subjs;

session2.SN.onevelnosac = ...
    (Session2.S2.SN.onevelnosac + ...
    Session2.S6.SN.onevelnosac)./s2_slowsubjs;

allsubj.SS.onevelnosac = ...
    (S2.SS.onevelnosac + ...
    S3.SS.onevelnosac + ...
    S4.SS.onevelnosac + ...
    S9.SS.onevelnosac)./slow_subjs;

session2.SS.onevelnosac = ...
    (Session2.S2.SS.onevelnosac + ...
    Session2.S6.SS.onevelnosac)./s2_slowsubjs;

% CREATE TARGET PATHS INCLUDING OCCLUDER POSITION FOR PLOTTING

targoccplot

% AVERAGE RMS ERRORS, GAINS AND VELOCITY ERRORS

allsubj.Stats.Fast.RMSE = ...
    (S2.Stats.RMSE(1:2,:) + ...
    S3.Stats.RMSE(1:2,:) + ...
    S4.Stats.RMSE(1:2,:) + ...
    S6.Stats.RMSE + ...
    S7.Stats.RMSE + ...
    S9.Stats.RMSE(1:2,:) + ...
    S10.Stats.RMSE + ...
    S11.Stats.RMSE)./fast_subjs;

allsubj.Stats.Fast.gain = ...
    (S2.Stats.gain(1:2,:) + ...
    S3.Stats.gain(1:2,:) + ...
    S4.Stats.gain(1:2,:) + ...
    S6.Stats.gain + ...
    S7.Stats.gain + ...
    S9.Stats.gain(1:2,:) + ...
    S10.Stats.gain + ...
    S11.Stats.gain)./fast_subjs;

allsubj.Stats.Fast.velerr = ...
    (S2.Stats.velerr(1:2,:) + ...
    S3.Stats.velerr(1:2,:) + ...
    S4.Stats.velerr(1:2,:) + ...
    S6.Stats.velerr + ...
    S7.Stats.velerr + ...
    S9.Stats.velerr(1:2,:) + ...
    S10.Stats.velerr + ...
    S11.Stats.velerr)./fast_subjs;

allsubj.Stats.Slow.RMSE = ...
    (S2.Stats.RMSE(3:4,:) + ...
    S3.Stats.RMSE(3:4,:) + ...
    S4.Stats.RMSE(3:4,:) + ...
    S9.Stats.RMSE(3:4,:))./slow_subjs;

allsubj.Stats.Slow.gain = ...
    (S2.Stats.gain(3:4,:) + ...
    S3.Stats.gain(3:4,:) + ...
    S4.Stats.gain(3:4,:) + ...
    S9.Stats.gain(3:4,:))./slow_subjs;

allsubj.Stats.Slow.velerr = ...
    (S2.Stats.velerr(3:4,:) + ...
    S3.Stats.velerr(3:4,:) + ...
    S4.Stats.velerr(3:4,:) + ...
    S9.Stats.velerr(3:4,:))./slow_subjs;

session2.Stats.Fast.RMSE = ...
    (Session2.S2.Stats.RMSE(1:2,:) + ...
    Session2.S3.Stats.RMSE + ...
    Session2.S4.Stats.RMSE + ...
    Session2.S9.Stats.RMSE)./s2_fastsubjs;

session2.Stats.Slow.RMSE = ...
    (Session2.S2.Stats.RMSE(3:4,:) + ...
    Session2.S6.Stats.RMSE)./s2_slowsubjs;

session2.Stats.Fast.gain = ...
    (Session2.S2.Stats.gain(1:2,:) + ...
    Session2.S3.Stats.gain + ...
    Session2.S4.Stats.gain + ...
    Session2.S9.Stats.gain)./s2_fastsubjs;

session2.Stats.Slow.gain = ...
    (Session2.S2.Stats.gain(3:4,:) + ...
    Session2.S6.Stats.gain)./s2_slowsubjs;

session2.Stats.Fast.velerr = ...
    (Session2.S2.Stats.velerr(1:2,:) + ...
    Session2.S3.Stats.velerr + ...
    Session2.S4.Stats.velerr + ...
    Session2.S9.Stats.velerr)./s2_fastsubjs;

session2.Stats.Slow.velerr = ...
    (Session2.S2.Stats.velerr(3:4,:) + ...
    Session2.S6.Stats.velerr)./s2_slowsubjs;

% CALCULATE LAGS

allsubj.FN.lag(1:2086,1)     = target22.X(1:2086,1) - allsubj.FN.one(1:2086,1);
allsubj.FN.lag(2087:4173,1)  = allsubj.FN.one(2087:4173,1) - target22.X(2087:4173,1);
allsubj.FS.lag(1:2086,1)     = target22.X(1:2086,1) - allsubj.FS.one(1:2086,1);
allsubj.FS.lag(2087:4173,1)  = allsubj.FS.one(2087:4173,1) - target22.X(2087:4173,1);

allsubj.SN.lag(1:2550,1)     = target18.X(1:2550,1) - allsubj.SN.one(1:2550,1);
allsubj.SN.lag(2551:5100,1)  = allsubj.SN.one(2551:5100,1) - target18.X(2551:5100,1);
allsubj.SS.lag(1:2550,1)     = target18.X(1:2550,1) - allsubj.SS.one(1:2550,1);
allsubj.SS.lag(2551:5100,1)  = allsubj.SS.one(2551:5100,1) - target18.X(2551:5100,1);

session2.FN.lag(1:2086,1)    = target22.X(1:2086,1) - session2.FN.one(1:2086,1);
session2.FN.lag(2087:4173,1) = session2.FN.one(2087:4173,1) - target22.X(2087:4173,1);
session2.FS.lag(1:2086,1)    = target22.X(1:2086,1) - session2.FS.one(1:2086,1);
session2.FS.lag(2087:4173,1) = session2.FS.one(2087:4173,1) - target22.X(2087:4173,1);

session2.SN.lag(1:2550,1)    = target18.X(1:2550,1) - session2.SN.one(1:2550,1);
session2.SN.lag(2551:5100,1) = session2.SN.one(2551:5100,1) - target18.X(2551:5100,1);
session2.SS.lag(1:2550,1)    = target18.X(1:2550,1) - session2.SS.one(1:2550,1);
session2.SS.lag(2551:5100,1) = session2.SS.one(2551:5100,1) - target18.X(2551:5100,1);

%% PLOT DISPLACEMENTS

figure(1)
subplot(2,1,1);
plot(allsubj.FS.one, '-r')
hold on
plot(allsubj.FN.one)
plot(target22.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all cycles','FontWeight','bold')

subplot(2,1,2);
plot(allsubj.SS.one, '-r')
hold on
plot(allsubj.SN.one)
plot(target18.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all cycles','FontWeight','bold')
%suplabel('Over all subjects','t')

% PLOT VELOCITIES

figure(2)
subplot(2,1,1);
plot(allsubj.FS.onevel, '-r')
hold on
plot(allsubj.FN.onevel)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold')

subplot(2,1,2);
plot(allsubj.SS.onevel, '-r')
hold on
plot(allsubj.SN.onevel)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold')
%suplabel('Over all subjects','t')


figure(3)
subplot(2,1,1);
plot(allsubj.FS.onevelnosac, '-r')
hold on
plot(allsubj.FN.onevelnosac)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (excluding saccades)','FontWeight','bold')

subplot(2,1,2);
plot(allsubj.SS.onevelnosac, '-r')
hold on
plot(allsubj.SN.onevelnosac)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (excluding saccades)','FontWeight','bold')
%suplabel('Over all subjects','t')

% PLOT RMSE'S OVERALL AND FOR IN/OUT OF OCCLUDER

FastLabel = {'22 deg/s Noisy';'22 deg/s Smooth'};
SlowLabel = {'18 deg/s Noisy';'18 deg/s Smooth'};

figure(4)
subplot(1,2,1)
bar(allsubj.Stats.Fast.RMSE)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('RMS error')
ylim([0 200]);
title('RMS errors for whole trials and occluded/visible periods','FontWeight','bold')
legend('RMSE for whole trial','RMSE for occluder',...
    'RMSE for visible target')

subplot(1,2,2)
bar(allsubj.Stats.Slow.RMSE)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('RMS error')
ylim([0 200]);
title('RMS errors for whole trials and occluded/visible periods','FontWeight','bold')
legend('RMSE for whole trial','RMSE for occluder',...
    'RMSE for visible target')
%suplabel('Over all subjects','t')
clear ylim

% PLOT GAINS

figure(5)
subplot(1,2,1)
bar(allsubj.Stats.Fast.gain)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('Gain (average eye/target velocity)')
ylim([0 0.9]);
title('Gain: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop gain','Resid pred gain (accel)',...
    'Resid pred gain (decel)')

subplot(1,2,2)
bar(allsubj.Stats.Slow.gain)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('Gain (average eye/target velocity)')
ylim([0 0.9]);
title('Gain: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop gain','Resid pred gain (accel)',...
    'Resid pred gain (decel)')
%suplabel('Over all subjects','t')
clear ylim

% PLOT MEAN VELOCITY ERRORS

figure(6)
subplot(1,2,1)
bar(allsubj.Stats.Fast.velerr)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('Mean velocity error (target - eye velocity)')
ylim = ([0 16]);
title('Mean velocity error: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop v error','Res pred v error (accel)',...
    'Res pred v error (decel)')
clear ylim

subplot(1,2,2)
bar(allsubj.Stats.Slow.velerr)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('Mean velocity error (target - eye velocity)')
ylim = ([0 16]);
title('Mean velocity error: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop v error','Res pred v error (accel)',...
    'Res pred v error (decel)')
%suplabel('Over all subjects','t')
clear ylim

% PLOT LAGS

figure(7)
subplot(2,1,1);
FNbar = bar(target22.time(1:4173,1), allsubj.FN.lag, 'FaceColor', 'b','EdgeColor', 'b');
FNbar = findobj(gca,'Type','patch');
set(FNbar,'facealpha', 0.1, 'edgealpha', 0.1);
hold on
FSbar = bar(target22.time(1:4173,1), allsubj.FS.lag, 'FaceColor', 'r','EdgeColor', 'r');
FSbar = findobj(gca,'Type','patch');
set(FSbar,'facealpha', 0.1, 'edgealpha', 0.1);
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target22.X(1:4173,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Lag of eye behind target','FontWeight','bold')
legend('22\circ/s Noisy','22\circ/s Smooth','Location','SouthEast')

subplot(2,1,2);
SNbar = bar(target18.time(1:5100,1), allsubj.SN.lag, 'FaceColor', 'b','EdgeColor', 'b');
SNbar = findobj(gca,'Type','patch');
set(SNbar,'facealpha', 0.1, 'edgealpha', 0.1);
hold on
SSbar = bar(target22.time(1:5100,1), allsubj.SS.lag, 'FaceColor', 'r','EdgeColor', 'r');
SSbar = findobj(gca,'Type','patch');
set(SSbar,'facealpha', 0.1, 'edgealpha', 0.1);
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target18.X(1:5100,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Lag of eye behind target','FontWeight','bold')
legend('18\circ/s Noisy','18\circ/s Smooth','Location','SouthEast')
%suplabel('Over all subjects','t')


%% PLOT SESSION 2 Data

figure(8)
subplot(2,1,1);
plot(session2.FS.one, '-r')
hold on
plot(session2.FN.one)
plot(target22.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean displacement over all cycles','FontWeight','bold')

subplot(2,1,2);
plot(session2.SS.one, '-r')
hold on
plot(session2.SN.one)
plot(target18.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean displacement over all cycles','FontWeight','bold')
%suplabel('Over all subjects','t')

% PLOT VELOCITIES

figure(9)
subplot(2,1,1);
plot(session2.FS.onevel, '-r')
hold on
plot(session2.FN.onevel)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean velocities over all cycles (incl. saccades: calculated from mean displacement)','FontWeight','bold')

subplot(2,1,2);
plot(session2.SS.onevel, '-r')
hold on
plot(session2.SN.onevel)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean velocities over all cycles (incl. saccades: calculated from mean displacement)','FontWeight','bold')
%suplabel('Over all subjects','t')


figure(10)
subplot(2,1,1);
plot(session2.FS.onevelnosac, '-r')
hold on
plot(session2.FN.onevelnosac)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean velocities over all cycles (excluding saccades)','FontWeight','bold')

subplot(2,1,2);
plot(session2.SS.onevelnosac, '-r')
hold on
plot(session2.SN.onevelnosac)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Session 2: Mean velocities over all cycles (excluding saccades)','FontWeight','bold')
%suplabel('Over all subjects','t')

% PLOT RMSE'S OVERALL AND FOR IN/OUT OF OCCLUDER

FastLabel = {'22 deg/s Noisy';'22 deg/s Smooth'};
SlowLabel = {'18 deg/s Noisy';'18 deg/s Smooth'};

figure(11)
subplot(1,2,1)
bar(session2.Stats.Fast.RMSE)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('RMS error')
ylim([0 200]);
title('Session 2: RMS errors for whole trials and occluded/visible periods','FontWeight','bold')
legend('RMSE for whole trial','RMSE for occluder',...
    'RMSE for visible target')

subplot(1,2,2)
bar(session2.Stats.Slow.RMSE)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('RMS error')
ylim([0 200]);
title('Session 2: RMS errors for whole trials and occluded/visible periods','FontWeight','bold')
legend('RMSE for whole trial','RMSE for occluder',...
    'RMSE for visible target')
%suplabel('Over all subjects','t')
clear ylim

% PLOT GAINS

figure(12)
subplot(1,2,1)
bar(session2.Stats.Fast.gain)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('Gain (average eye/target velocity)')
ylim([0 0.9]);
title('Session 2 Gain: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop gain','Resid pred gain (accel)',...
    'Resid pred gain (decel)')

subplot(1,2,2)
bar(session2.Stats.Slow.gain)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('Gain (average eye/target velocity)')
ylim([0 0.9]);
title('Session 2 Gain: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop gain','Resid pred gain (accel)',...
    'Resid pred gain (decel)')
%suplabel('Over all subjects','t')
clear ylim

% PLOT MEAN VELOCITY ERRORS

figure(13)
subplot(1,2,1)
bar(session2.Stats.Fast.velerr)
set(gca,'XTickLabel',FastLabel)
xlabel('Trial types')
ylabel('Mean velocity error (target - eye velocity)')
ylim = ([0 16]);
title('Session 2 Mean velocity error: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop v error','Res pred v error (accel)',...
    'Res pred v error (decel)')
clear ylim

subplot(1,2,2)
bar(session2.Stats.Slow.velerr)
set(gca,'XTickLabel',SlowLabel)
xlabel('Trial types')
ylabel('Mean velocity error (target - eye velocity)')
ylim = ([0 16]);
title('Session 2 Mean velocity error: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop v error','Res pred v error (accel)',...
    'Res pred v error (decel)')
%suplabel('Over all subjects','t')
clear ylim

% PLOT LAGS

figure(14)
subplot(2,1,1);
FNbar = bar(target22.time(1:4173,1), session2.FN.lag, 'FaceColor', 'b','EdgeColor', 'b');
FNbar = findobj(gca,'Type','patch');
set(FNbar,'facealpha', 0.1, 'edgealpha', 0.1);
hold on
FSbar = bar(target22.time(1:4173,1), session2.FS.lag, 'FaceColor', 'r','EdgeColor', 'r');
FSbar = findobj(gca,'Type','patch');
set(FSbar,'facealpha', 0.1, 'edgealpha', 0.1);
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target22.X(1:4173,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Session 2: Lag of eye behind target','FontWeight','bold')
legend('22\circ/s Noisy','22\circ/s Smooth','Location','SouthEast')

subplot(2,1,2);
SNbar = bar(target18.time(1:5100,1), session2.SN.lag, 'FaceColor', 'b','EdgeColor', 'b');
SNbar = findobj(gca,'Type','patch');
set(SNbar,'facealpha', 0.1, 'edgealpha', 0.1);
hold on
SSbar = bar(target22.time(1:5100,1), session2.SS.lag, 'FaceColor', 'r','EdgeColor', 'r');
SSbar = findobj(gca,'Type','patch');
set(SSbar,'facealpha', 0.1, 'edgealpha', 0.1);
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target18.X(1:5100,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Session 2: Lag of eye behind target','FontWeight','bold')
legend('18\circ/s Noisy','18\circ/s Smooth','Location','SouthEast')
%suplabel('Over all subjects','t')

%% SAVE DATA

save([savefolder 'AllSubjects.mat'])

clear savefolder FastLabel SlowLabel a i t